#include "fun.h"
#include "iic.h"
#include "ds1302.h"
#include "onewire.h"

u8 bdata LED;
sbit L1 = LED^0;sbit L2 = LED^1;sbit L3 = LED^2;sbit L4 = LED^3;
sbit L5 = LED^4;sbit L6 = LED^5;sbit L7 = LED^6;sbit L8 = LED^7;
u8 time_param=17,tem_param=25,led_param=4,last_time_param=17,last_tem_param=25,last_led_param=4;
u8 param_flag,data_flag,RD1_flag,last_RD1_flag,h,m,s;
bit disp_status;
u16 L3_time = 0;
u8 key_count;
u8 index,i,t_100ms = 1,L3_flag = 1;
u8 code t_display[]={                       //
//   0    1    2    3    4    5    6    7    8    9    
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,
		0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,	//0. 1. 2. 3. 4. 5. 6. 7. 8. 9.
	  0x39,0x79,0x73,0x00,0x40};// C   E   P  black - 

u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};    
u8 data_disp[][8] = 
{
	{0,0,24,0,0,24,0,0},
	{20,23,23,23,23,0,0,0},
	{21,23,0,0,0,23,23,0},
  {22,0,23,23,23,23,0,0}
};

void Timer2Init(void)		//1ms@12.000MHz
{
	AUXR |= 0x04;		//?????1T??
	T2L = 0x20;		//??????
	T2H = 0xD1;		//??????
	AUXR |= 0x10;		//???2????
	IE2 |= 0x04;
	EA = 1;
}

void main(void)
{
	u8 j = 10;
	u16 adc;
	buzzer(0);
	relay(0);
	led(LED);
	
	while(j--)
		read_tem();
	set_clock(16,59,50);
	Timer2Init();

	while(1)
	{
		if(t_100ms)
		{
			t_100ms = 0;
			EA = 0;
			h = Read_Ds1302_Byte(0x85);
			m = Read_Ds1302_Byte(0x83);
			s = Read_Ds1302_Byte(0x81);
			EA = 1;
			
			read_tem();
			adc = read_adc();
			adc = adc * 500 / 255.0;
			if(adc > 50)
				RD1_flag = 0;		//0表示亮
			else
				RD1_flag = 1;
			
			if(last_RD1_flag != RD1_flag)
			{
				L3_flag = 1;
				L3_time = 0;
			}

			last_RD1_flag = RD1_flag;
		}

		if(!disp_status)
		{
			if(data_flag == 0)		//时间数据
			{
				index = 0;
				data_disp[index][0] = h / 16;
				data_disp[index][1] = h % 16;
				data_disp[index][3] = m / 16;
				data_disp[index][4] = m % 16;
				data_disp[index][6] = s / 16;
				data_disp[index][7] = s % 16;
			}
			else if(data_flag == 1)
			{
				index = 1;
				data_disp[index][5] = temperature / 100;
				data_disp[index][6] = ((temperature % 100) / 10) + 10;
				data_disp[index][7] = temperature % 10;
			}
			else if(data_flag == 2)
			{
				index = 2;
				data_disp[index][2] = (adc / 100) + 10;
				data_disp[index][3] = (adc % 100) / 10; 
				data_disp[index][4] = adc % 10;
				data_disp[index][7] = RD1_flag;
			}
		}
		else
		{
			index = 3;
			if(param_flag == 0)			//时间
			{
				data_disp[index][1] = 1;
				data_disp[index][6] = time_param / 10; 
				data_disp[index][7] = time_param % 10;
			}
			else if(param_flag == 1)		//温度
			{
				data_disp[index][1] = 2;
				data_disp[index][6] = tem_param / 10; 
				data_disp[index][7] = tem_param % 10;
			}
			else if(param_flag == 2)		//指示灯参数
			{
				data_disp[index][1] = 3;
				data_disp[index][6] = 23; 
				data_disp[index][7] = led_param;
			}
		}
	}
}

void t2_callback(void) interrupt 12
{
	static u8 t = 0;
	t++;
	if(t >= 100)
	{
		t_100ms = 1; 
		t = 0; 
	}
	if(++key_count == 10)
	{
		key_count = 0;
		key_scan();
	}

	if(L3_flag)
	{
		L3_time++;
		if(L3_time >= 3000)
		{
			L3_time = 0;
			if(!RD1_flag)
				L3 = 0;
			else
				L3 = 1;
			L3_flag = 0;
		}
	}
	else
		L3_time = 0;
	
	
	if((h/16 * 10) + h%16 >= last_time_param)
		L1 = 1;
	else 
		L1 = 0;

	if(temperature < last_tem_param * 10)
		L2 = 1;
	else
		L2 = 0;
	
	if(RD1_flag)
	{
		LED &= 0x07;
		LED = LED | (0x01 << (last_led_param - 1));
	}
	else
		LED &= 0x07;
	
	led(LED);
	i %= 8;
	smg_disp(t_display[data_disp[index][i]],T_COM[i]);
	i++;
}
